package org.come.npc;

import com.tool.role.RoleData;
import org.come.Frame.NPCJfram;
import org.come.Frame.TestsmallmapJframe;
import org.come.Frame.WorldTestsmallmapJframe;
import org.come.Frame.ZhuFrame;
import org.come.Jpanel.NPCJpanel;
import org.come.bean.AllMapBean;
import org.come.bean.AllTitleBean;
import org.come.bean.ChangeMapBean;
import org.come.bean.TeamBean;
import org.come.entity.Goodstable;
import org.come.entity.TeamRole;
import org.come.model.Door;
import org.come.model.Gamemap;
import org.come.model.Title;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.thread.TimeControlRunnable;
import org.come.until.GsonUtil;
import org.come.until.UserMessUntil;
import org.come.until.Util;

import java.util.List;

import com.tool.ModerateTask.TaskMixDeal;
import com.tool.image.ImageMixDeal;
import come.tool.Fighting.FightingMixDeal;
import come.tool.handle.HandleState;

import javax.management.relation.Role;
import java.util.List;
import java.util.Map;

/**
 * 传送
 *
 * @author Administrator
 */
public class TP {
	/**
	 * 判断点击是否为tp
	 */
	public static boolean isTP(String msg) {
		NPCJpanel npcjpanel = NPCJfram.getNpcJfram().getNpcjpanel();
		if (npcjpanel.getNpcInfoBean() == null) {
			return false;
		}
		if (npcjpanel.getNpcInfoBean().getDoors() == null) {
			return false;
		}
		for (int i = 0; i < npcjpanel.getNpcInfoBean().getDoors().size(); i++) {
			if (npcjpanel.getNpcInfoBean().getDoors().get(i).getDoorkey().equals(msg)) {
				tp(npcjpanel.getNpcInfoBean().getDoors().get(i), 2);
				return true;
			}
		}
		return false;
	}

	/**
	 * door生成
	 */
	public static Door scdoor(String mapid, int x, int y) {
		Door door = new Door();
		door.setDoormap(mapid);
		door.setDoorpoint(x + "|" + y);
		return door;
	}

	/**
	 * 给传送旗做路标
	 * 0是旧旗子 其他数是新旗子的使用次数
	 */
	public static void FlightRedraw(Goodstable good, int leixing) {
		//if (!TaskMixDeal.istp()) {
		// ZhuFrame.getZhuJpanel().addPrompt2("传送限制");
		// return;
		// }
		if (ImageMixDeal.userimg.getRoleShow().getBooth_id() != null) {
			ZhuFrame.getZhuJpanel().addPrompt2("摆摊状态不可以飞行！");
			return;
		}
		String flag = Util.mapmodel.getGamemap().getMapflag();
		if (flag == null || !"1".equals(flag) || ImageMixDeal.userimg.getTeams() == null) {
			ZhuFrame.getZhuJpanel().addPrompt2("飞行棋使用限制");
			return;
		}
		//获取当前的位置信息
		String path = ImageMixDeal.userimg.getRoleShow().getMapid() + "," + Util.mapmodel.getGamemap().getMapname() + "," + ImageMixDeal.userimg.getRoleShow().getX() + "," + ImageMixDeal.userimg.getRoleShow().getY();
		if (leixing == 0) {
			String[] vs = good.getValue().split("\\|");
			good.setValue("传送=" + path + "|" + vs[1]);
		} else {
			good.setValue("传送=" + path + "|可用次数=" + leixing);
		}
		ZhuFrame.getZhuJpanel().addPrompt2("已经重新做好路标");

		TestsmallmapJframe.getTestsmallmapJframe().getTestsmallmapJpanel().clearTps();
		WorldTestsmallmapJframe.getWorldTestsmallmapJframe().getWorldTestsmallmapJpanel().clearTps();
	}


	/**
	 * 0是传送门传送
	 * 1是传送器传送
	 * 2是NPC传送
	 * 3是脚本传送
	 * 4是进入帮派
	 *
	 * @param door
	 */
	public static boolean isfly = false;

	public static void tp(Door door, int type) {
		if (ImageMixDeal.userimg.getRoleShow().getBooth_id() != null) {
			ZhuFrame.getZhuJpanel().addPrompt2("摆摊状态不可以飞行！");
			return;
		}
		if (door == null) {
			return;
		}
		if (type != 3 && type != 0) {
			TimeControlRunnable.removeScript();
		}
		if (FightingMixDeal.State != HandleState.USUAL) {
			ZhuFrame.getZhuJpanel().addPrompt2("你在战斗中");
			return;
		}

		RoleData roleData = RoleData.getRoleData();
		//system.out.println("Rolename:"+ roleData.getLoginResult().getRolename() + " id:" + roleData.getLoginResult().getRole_id() + "&id:" + roleData.getLoginResult().getSpecies_id());

		//复制写给客户端的流
//		ChangeMapBean changeMapBean=new ChangeMapBean();
//		changeMapBean.setMapid(door.getDoormap());
//		String dxy=door.getDoorpoint();
//		String a[] = dxy.split("\\|");
//		int dx=Integer.parseInt(a[0]);
//		int dy=Integer.parseInt(a[1]);
//		changeMapBean.setMapx(dx);
//		changeMapBean.setMapy(dy);
//		changeMapBean.setType(type);
//		if(changeMapBean.getType() == 2) {
//			AllMapBean map = UserMessUntil.getAllmapbean();
//			Map<String, Gamemap> maps = map.getAllMap();
//			Gamemap m = maps.get(door.getDoormap());
//			if(m.getTitle()!=null && !m.getTitle().equals("")) {
//				AllTitleBean tall = UserMessUntil.getAllTitle();
//				Map<String, Title> titleMap = tall.getTitleMap();
//				String titlename = "";
//				for (Map.Entry<String, Title> entry : titleMap.entrySet()) {
//					Title yl = entry.getValue();
//					if(m.getTitle().toString().equals(yl.getId().toString())){
//						titlename = yl.getTitlename();
//					}
//				}
		ChangeMapBean changeMapBean = new ChangeMapBean();
		changeMapBean.setMapid(door.getDoormap());
		String dxy = door.getDoorpoint();
		String a[] = dxy.split("\\|");
		int dx = Integer.parseInt(a[0]);
		int dy = Integer.parseInt(a[1]);
		changeMapBean.setMapx(dx);
		changeMapBean.setMapy(dy);
		changeMapBean.setType(type);
		if (changeMapBean.getType() == 2) {
			AllMapBean map = UserMessUntil.getAllmapbean();
			Map<String, Gamemap> maps = map.getAllMap();
			Gamemap m = maps.get(door.getDoormap());
			if (m.getTitle() != null && !m.getTitle().equals("")) {
				AllTitleBean tall = UserMessUntil.getAllTitle();
				Map<String, Title> titleMap = tall.getTitleMap();
				String titlename = "";
				for (Map.Entry<String, Title> entry : titleMap.entrySet()) {
					Title yl = entry.getValue();
					if (m.getTitle().toString().equals(yl.getId().toString())) {
						titlename = yl.getTitlename();
					}
				}
//				if(!(RoleData.getRoleData().getLoginResult().getTitle()!=null &&  RoleData.getRoleData().getLoginResult().getTitle()!="" &&RoleData.getRoleData().getLoginResult().getTitle().equals(titlename))){
//					ZhuFrame.getZhuJpanel().addPrompt2("进入此地图需要要佩戴称谓【"+titlename+"】!");
//					return;
//				}//这段是之前的 只能单人控制检测佩戴称谓进地图。下面是我自己加的。

				// 获取当前玩家的称谓
				String playerTitle = RoleData.getRoleData().getLoginResult().getTitle();

				// 检查当前玩家的称谓是否满足要求
				if (playerTitle == null || playerTitle.isEmpty() || !playerTitle.equals(titlename)) {
					ZhuFrame.getZhuJpanel().addPrompt2("进入此地图需要佩戴称谓【" + titlename + "】!");
					return;
				}
			}
		}
		String sendMes = Agreement.getAgreement().ChangemapAgreement(GsonUtil.getGsonUtil().getgson().toJson(changeMapBean));
		//向服务器发送信息
		SendMessageUntil.toServer(sendMes);

		TestsmallmapJframe.getTestsmallmapJframe().getTestsmallmapJpanel().clearTps();
		WorldTestsmallmapJframe.getWorldTestsmallmapJframe().getWorldTestsmallmapJpanel().clearTps();
	}

	public static void tp(int map, int x, int y) {
		if (ImageMixDeal.userimg.getRoleShow().getBooth_id() != null) {
			ZhuFrame.getZhuJpanel().addPrompt2("摆摊状态不可以飞行！");
			return;
		}
		if (FightingMixDeal.State != HandleState.USUAL) {
			ZhuFrame.getZhuJpanel().addPrompt2("你在战斗中");
			return;
		}
		//复制写给客户端的流
		ChangeMapBean changeMapBean = new ChangeMapBean();
		changeMapBean.setMapid(map + "");
		changeMapBean.setMapx(x);
		changeMapBean.setMapy(y);
		changeMapBean.setType(3);
		String sendMes = Agreement.getAgreement().ChangemapAgreement(GsonUtil.getGsonUtil().getgson().toJson(changeMapBean));
		SendMessageUntil.toServer(sendMes);
	}
}
